package com.sinopac.action.CustomerApply;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

import com.sinopac.actionCore.ActionCore;
import com.sinopac.entity.CustomerApply.CustomerVO;
import com.sinopac.entity.CustomerApply.ReportVO;
import com.sinopac.model.CustomerApply.CustomerApplyModel;
import com.sinopac.model.ccroger.CcrogerModel;

@Namespace("/CustomerApply")
//@ParentPackage("struts-default")
public class CustomerApplyAction extends ActionCore{
	private static final long serialVersionUID = 1L;
	private List<CustomerVO> customerList;
	private String custId;
	//index
	private String customer_contact;
	private String customer_phone;
	private String customer_address;
	private String mailarea;
	private String mailbarcode;
	//allotmentToken
	private String token_esn;
	private Boolean token_flag;
	private List<CustomerVO> groupList;
	private String token_msg;
	private String subdate;
	//report
	private List<ReportVO> reportList = new ArrayList<ReportVO>();
	
	@Action(value="index",results={@Result(name="input",location="/form/CustomerApply/CustomerApply.jsp")})
	public String index(){
		token_flag = false;
		CustomerApplyModel cam = new CustomerApplyModel();
//		if(StringUtils.isNotBlank(custId)){
//			session.put(CustomersID, custId);
//		}else{
//			session.put(CustomersID, "12345cn");
//		}
		if(StringUtils.isNotBlank((String)session.get("customer_id"))){
			Boolean flag = cam.chkJobQs((String)session.get("customer_id"));
			if(flag){
				token_msg = "該客戶申請TOKEN配發中...";
				token_flag = true;
			}
//			System.out.println("customer_idcustomer_id:" + (String)session.get("customer_id"));
			customerList = cam.getCustomerForm((String)session.get("customer_id"));
		}
		return INPUT;
	}
	
	@Action(value="modifyData",results={@Result(name="success",type="chain",location="index")})
	public String modifyData(){
		CustomerApplyModel cam = new CustomerApplyModel();
		cam.modifyData((String)session.get(CustomersID), customer_contact, customer_phone, customer_address, mailarea, mailbarcode);
		return SUCCESS;
	}
	
	@Action(value="allotmentToken",results={@Result(name="input",location="/form/CustomerApply/tokenApply.jsp"),
//											@Result(name="success",type="chain",location="allotmentTokenSuccess"),
											@Result(name="success",location="/form/CustomerApply/tokenSuccess.jsp"),
											@Result(name="error",type="chain",location="index")})
	public String allotmentToken(){
		token_flag = false;
		String customer_id = (String)session.get(CustomersID);
		if((String)session.get("token_esn") !=null){
			token_esn = (String)session.get("token_esn");
		}
		System.out.println("tokenEsn:" + token_esn);
		if(StringUtils.isNotBlank(token_esn)){
//			System.out.println("token_esn:" + token_esn);
//			String customer_id = (String)session.get(CustomersID);
			CustomerApplyModel cam = new CustomerApplyModel();
			String error_msg = cam.chkToken(customer_id,token_esn);
			if(StringUtils.isBlank(error_msg)){
				customerList = cam.getCustomerForm(customer_id);
				groupList = cam.getGroupList(customer_id);
				return SUCCESS;
			}else{
				token_msg = error_msg;
				token_flag = true;
			}
		}else{
			CcrogerModel cm = new CcrogerModel();
			token_esn = cm.getToken();
		}
//		else{
//			CustomerApplyModel cam = new CustomerApplyModel();
//			if(cam.chkTokenApply((String)session.get("customer_id"))){
//				return INPUT;
//			}else{
//				return ERROR;
//			}
//		}
		return INPUT;
	}
	
//	@Action(value="allotmentTokenSuccess",results={@Result(name="success",location="/form/CustomerApply/tokenSuccess.jsp")})
//	public String allotmentTokenSuccess(){
//		token_flag = false;
//		if(StringUtils.isNotBlank((String)session.get(CustomersID))){
//			CustomerApplyModel cam = new CustomerApplyModel();
//			customerList = cam.getCustomerForm((String)session.get(CustomersID));
//			GroupList = cam.getGroupList((String)session.get(CustomersID));
//			String token_esn = StringUtils.isNotBlank((String)session.get("token_esn"))?(String)session.get("token_esn"):this.token_esn;
//			cam.insertJobQs(customerList, token_esn, session.get("userName").toString());
//		}
//		return SUCCESS;
//	}
	
	@Action(value="printRoport",results={@Result(name="success",type="chain",location="backMain")})
	public String printRoport(){
		token_flag = false;
		Date date = new Date();
//		System.out.println("token_esn:" + token_esn);
		if(StringUtils.isNotBlank((String)session.get(CustomersID))){
			CustomerApplyModel cam = new CustomerApplyModel();
			String token_esn = StringUtils.isNotBlank((String)session.get("token_esn"))?(String)session.get("token_esn"):this.token_esn;			
			cam.getReport((String)session.get(CustomersID), token_esn, session.get("userName").toString(),reportList,date);
			String pdfPath = cam.generatePDF(reportList);
			session.put("pdfPath", pdfPath);
		}
		return SUCCESS;
	}
	
	@Action(value="idQuery",results={@Result(name="input",location="/form/CustomerApply/CustomerQuery.jsp")})
	public String idQuery(){
		CustomerApplyModel cam = new CustomerApplyModel();

		if(StringUtils.isNotBlank((String)session.get("customer_id"))){
			customerList = cam.getCustomerQuery((String)session.get("customer_id"));
		}
		return INPUT;
	}
	
	@Action(value="cancelPrint",results={@Result(name="success",type="chain",location="backMain")})
	public String cancelPrint(){
		CustomerApplyModel cam = new CustomerApplyModel();
		if(StringUtils.isNotBlank(subdate)){
			cam.deleteDB(subdate);
		}
		return SUCCESS;
	}
	
	@Action(value="updateData",results={@Result(name="success",type="json",params={"root","jsonResult"})})
	public String updateData(){
		token_esn = ServletActionContext.getRequest().getParameter("token_esn");
		customer_contact = ServletActionContext.getRequest().getParameter("customer_contact");
		customer_phone = ServletActionContext.getRequest().getParameter("customer_phone");
		customer_address = ServletActionContext.getRequest().getParameter("customer_address");
		mailarea = ServletActionContext.getRequest().getParameter("mailarea");
		mailbarcode = ServletActionContext.getRequest().getParameter("mailbarcode");
		custId = (String)session.get(CustomersID);
		System.out.println("customer_address===" + customer_address);
		if(StringUtils.isNotBlank(custId)){
			CustomerApplyModel cam = new CustomerApplyModel();
			if(token_esn !=null){
				String error_msg = cam.chkToken(custId,token_esn);
				if(StringUtils.isNotBlank(error_msg)){
					setJsonResult(error_msg);
					return SUCCESS;
				}
			}
			boolean flag = cam.modifyData(custId, customer_contact, customer_phone, customer_address, mailarea, mailbarcode);
			if(flag==false){
				return ERROR;
			}
			setJsonResult("");
		}
		return SUCCESS;
	}

	public List<CustomerVO> getCustomerList() {
		return customerList;
	}

	public void setCustomerList(List<CustomerVO> customerList) {
		this.customerList = customerList;
	}

	public String getCustomer_contact() {
		return customer_contact;
	}

	public void setCustomer_contact(String customer_contact) {
		this.customer_contact = customer_contact;
	}

	public String getCustomer_phone() {
		return customer_phone;
	}

	public void setCustomer_phone(String customer_phone) {
		this.customer_phone = customer_phone;
	}

	public String getCustomer_address() {
		return customer_address;
	}

	public void setCustomer_address(String customer_address) {
		this.customer_address = customer_address;
	}

	public String getToken_esn() {
		return token_esn;
	}

	public void setToken_esn(String token_esn) {
		this.token_esn = token_esn;
	}

	public Boolean getToken_flag() {
		return token_flag;
	}

	public void setToken_flag(Boolean token_flag) {
		this.token_flag = token_flag;
	}

	public List<CustomerVO> getGroupList() {
		return groupList;
	}

	public void setGroupList(List<CustomerVO> groupList) {
		this.groupList = groupList;
	}

	public String getToken_msg() {
		return token_msg;
	}

	public void setToken_msg(String token_msg) {
		this.token_msg = token_msg;
	}

	public String getCustId() {
		return custId;
	}

	public void setCustId(String custId) {
		this.custId = custId;
	}

	public String getSubdate() {
		return subdate;
	}

	public void setSubdate(String subdate) {
		this.subdate = subdate;
	}

	public List<ReportVO> getReportList() {
		return reportList;
	}

	public void setReportList(List<ReportVO> reportList) {
		this.reportList = reportList;
	}

	public String getMailarea() {
		return mailarea;
	}

	public void setMailarea(String mailarea) {
		this.mailarea = mailarea;
	}

	public String getMailbarcode() {
		return mailbarcode;
	}

	public void setMailbarcode(String mailbarcode) {
		this.mailbarcode = mailbarcode;
	}

}
